package services

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"go_motor_catalog/dao"
)

type HondaOm struct {
}

type HondaOmRes struct {
	ModelName  string
	ModelYear  string
	CatalogUrl string
}

func NewHondaOm() *HondaOm {
	return &HondaOm{}
}

func (h *HondaOm) getCatalogs(db *gorm.DB) []*HondaOmRes {
	res := make([]*HondaOmRes, 0)
	modelDao := dao.NewFilterProductModelDao(db)
	productDao := dao.NewFilterProductDao(db)
	catalogDao := dao.NewFilterProductCatalogDao(db)

	models := modelDao.GetListByProductId("4")
	for i := range models {
		model := models[i]
		products := productDao.GetListByModelId(fmt.Sprintf("%v", model.Id))
		for p := range products {
			product := products[p]
			catalogs := catalogDao.GetListByProductId(fmt.Sprintf("%v", product.Id))
			for c := range catalogs {
				catalog := catalogs[c]
				res = append(res, &HondaOmRes{
					ModelName:  product.ModelName,
					ModelYear:  product.ModelYear,
					CatalogUrl: catalog.FigBranchNo,
				})
			}
		}
	}
	return res
}

func (h *HondaOm) SyncHondaCatalogs(db *gorm.DB) {
	catalogs := h.getCatalogs(db)
	modelDao := dao.NewManualModelOmDao(db)
	cataLogDao := dao.NewManualCatalogOmDao(db)

	for i := range catalogs {
		catalog := catalogs[i]
		modelId := modelDao.CreateManualModelOm(&dao.ManualModelOm{
			ModelName: catalog.ModelName,
			ModelYear: catalog.ModelYear,
			BrandId:   4,
		})
		cataLogDao.CreateManualCatalogOm(&dao.ManualCatalogOm{
			CatalogUrl: catalog.CatalogUrl,
			ModelId:    modelId,
		})
		fmt.Println(catalog.ModelName, "-", catalog.ModelYear, "-successfull")
	}
	fmt.Println("honda successfull")
}
